package cn.stylefeng.guns.modular.ums.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.ChatPreset;
import cn.stylefeng.guns.modular.ums.mapper.ChatPresetMapper;
import cn.stylefeng.guns.modular.ums.model.params.ChatPresetParam;
import cn.stylefeng.guns.modular.ums.model.result.ChatPresetResult;
import  cn.stylefeng.guns.modular.ums.service.ChatPresetService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 聊天预设话术 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-05-17
 */
@Service
public class ChatPresetServiceImpl extends ServiceImpl<ChatPresetMapper, ChatPreset> implements ChatPresetService {

    @Override
    public void add(ChatPresetParam param){
        ChatPreset entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ChatPresetParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ChatPresetParam param){
        ChatPreset oldEntity = getOldEntity(param);
        ChatPreset newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ChatPresetResult findBySpec(ChatPresetParam param){
        //查询回复信息
        List<ChatPresetResult> list=findListBySpec(param);
        if (list.size() > 0) {
            return  list.get(0);
        }
        return null;
    }

    @Override
    public List<ChatPresetResult> findListBySpec(ChatPresetParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ChatPresetParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ChatPresetParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ChatPreset getOldEntity(ChatPresetParam param) {
        return this.getById(getKey(param));
    }

    private ChatPreset getEntity(ChatPresetParam param) {
        ChatPreset entity = new ChatPreset();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
